<?php

namespace app\store\controller\market\card;

use app\store\controller\Controller;
use app\store\model\card\Card as CardModel;
use app\store\model\Order as OrderModel;
/**
 * 秒杀商品管理
 * Class Goods
 * @package app\store\controller\apps\sharp
 */
class Card extends Controller
{
    /**
     * 储值卡列表
     * @param string $search
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function index($search = '')
    {
        $model = new CardModel;
        $list = $model->getList($search);
        return $this->fetch('index', compact('list'));
    }

    /**
     * 添加秒杀商品
     * @param int $step
     * @param null $goods_id
     * @return array|bool|mixed
     * @throws \Exception
     */
    public function add()
    {
        $model = new CardModel;
        // 验证能否添加
        if (!$this->request->isAjax()) {
            return $this->fetch(
                'add'
            );
        }
 
        if ($model->add($this->postData('card'))) {
            return $this->renderSuccess('添加成功', url('market.card.card/index'));
        }
        return $this->renderError($model->getError() ?: '添加失败');
    }


    /**
     * 编辑
     * @param $card_id
     * @return array|bool|mixed
     * @throws \think\exception\DbException
     * @throws \Exception
     */
    public function edit($card_id)
    {
        // 商品详情
        $model = CardModel::detail($card_id);
        if (!$this->request->isAjax()) {
            return $this->fetch(
                'edit',
                array_merge(compact('model'))
            );
        }
//        $realPayMoney = current($this->postData('card.card_real_money'));
//        if(bccomp($realPayMoney, '5000', 2) == 1) {
//            return $this->renderError('实付金额不能大于5000元');
//        }
        // 更新记录
        if ($model->edit($this->postData('card'))) {
            return $this->renderSuccess('更新成功', url('market.card.card/index'));
        }
        return $this->renderError($model->getError() ?: '更新失败');
    }

    /**
     * 删除储值卡
     * @param $card_id
     * @return array
     */
    public function delete($card_id)
    {
        // 商品详情
        $model = CardModel::detail($card_id);
        if (!$model->setDelete()) {
            return $this->renderError($model->getError() ?: '删除失败');
        }
        return $this->renderSuccess('删除成功');
    }

    /**
     * 储值卡订单列表
     * @param string $search
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function order()
    {
        // 订单列表
        $model = new OrderModel;
        $list = $model->cardOrder($this->request->param());
        return $this->fetch('order', compact('list'));
    }

    /**
     * 订单导出
     * @param string $dataType
     * @throws \think\exception\DbException
     */
    public function export()
    {
        $model = new OrderModel;

        if (!$model->exportCardList($this->request->param())) {
            return $this->renderError($model->getError() ?: '操作失败');
        }

        return $model->exportCardList($this->request->param());
    }


    /**
     * 储值卡订单列表
     * @param string $search
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function del($order_id)
    {
        $model = OrderModel::detail($order_id);

        if(!$model){
            return $this->renderError($model->getError() ?: '订单不存在,请刷新后重试');
        }
        if($model['pay_status']['value']==20){
            return $this->renderError('该订单禁止删除');
        }

        if ($model->orderDel()) {
            return $this->renderSuccess('删除成功');
        }
        return $this->renderError($model->getError() ?: '删除失败');
    }


}